package com.qingcheng.timer;

import com.qingcheng.dao.SeckillGoodsMapper;
import com.qingcheng.pojo.seckill.SeckillGoods;
import com.qingcheng.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.xml.ws.Endpoint;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * @Author: LDeng
 * @Date: 2020-07-10 11:55
 */

@Component
public class SeckillGoodsTask {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Scheduled(cron="0-5 * * * * ?")
    public void loadGoods(){

//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        System.out.println(simpleDateFormat.format(new Date()));

        //1 查询所有时间区间

        List<Date> dateMenus =DateUtil.getDateMenus();
        //2 循环时间区间，查询每个时间区间的秒杀商品
        for(Date startTime:dateMenus){
            Example example=new Example(SeckillGoods.class);
            Example.Criteria criteria=example.createCriteria();
            criteria.andEqualTo("status","1");
            criteria.andGreaterThanOrEqualTo("stockCount",0);
            criteria.andGreaterThanOrEqualTo("startTime",DateUtil.addDateHour(startTime,0));
            criteria.andLessThanOrEqualTo("endTime",DateUtil.addDateHour(startTime,2));
            //System.out.println("debug============>"+startTime);

            //2.4 过滤Redis中已经存在的该区间的秒杀商品
            Set keys = redisTemplate.boundHashOps("SeckillGoods_" + DateUtil.date2Str(startTime)).keys();
            if(keys!=null && keys.size()>0){
                //select * from table where id not in(keys)
                criteria.andNotIn("id",keys);
            }

            //2.4 执行查询
            List<SeckillGoods> seckillGoods = seckillGoodsMapper.selectByExample(example);

            for (SeckillGoods seckillGood : seckillGoods) {
                System.out.println(DateUtil.date2Str(startTime));
                //完整数据
                redisTemplate.boundHashOps("SeckillGoods_"+DateUtil.date2Str(startTime)).put(seckillGood.getId(),seckillGood);
                //剩余库存个数seckillGood.getStockCount()=5
                // 创建独立队列： 存储商品剩余库存
                //SeckillGoodsList_ID():
                // 5个商品就存5个数字  [10,10,10,10,10];
                Long[] ids = pushIds(seckillGood.getStockCount(), seckillGood.getId());
                System.out.println("商品id:"+seckillGood.getId()+" 压入REDIS队列的元素数量为："+ids.length);
                //创建队列
                redisTemplate.boundListOps("SeckillGoodsCountList_"+seckillGood.getId()).leftPushAll(ids);

                //创建自增KEY的值
                redisTemplate.boundHashOps("SeckillGoodsCount").increment(seckillGood.getId(),seckillGood.getStockCount());

            }

        }

                //2.1 商品必须审核通过
                //2.2 库存必须》0
                //2.3 秒杀开始时间必须>=当前循环时间区间的开始时间
                //2.3 秒杀结束时间<当前循环时间区间的开始时间+2

        //3 将秒杀商品存入到Redis缓存

    }


    //组装商品ID，将商品ID组装成库存数量个数数组
    //param: len 为商品个数
    //param： id 为商品id

    public Long[] pushIds(int len,Long id){

        Long[] ids=new Long[len];
        for (int i = 0; i <len; i++) {
            ids[i]=id;
        }
        return ids;
    }



}
